package com.sinothk.fc.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.customer.entity.CustomerEntity;
import com.sinothk.fc.customer.entity.CustomerReq;
import com.sinothk.fc.customer.service.ICustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 门店客户表
 * @Author: Liangyt
 * @Date: 2022-09-11
 * @Version: V1.0
 */
@Api(tags = "门店客户表")
@RestController
@RequestMapping("/fc/customerCtrl")
@Slf4j
public class CustomerController extends JeecgController<CustomerEntity, ICustomerService> {

    private final ICustomerService consumerService;

    public CustomerController(ICustomerService consumerService) {
        this.consumerService = consumerService;
    }

    /**
     * 分页列表查询
     *
     * @param customerEntity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "门店客户表-分页列表查询")
    @ApiOperation(value = "门店客户表-分页列表查询", notes = "门店客户表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CustomerEntity customerEntity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CustomerEntity> queryWrapper = QueryGenerator.initQueryWrapper(customerEntity, req.getParameterMap());
        Page<CustomerEntity> page = new Page<CustomerEntity>(pageNo, pageSize);
        IPage<CustomerEntity> pageList = consumerService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param customerReq
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "门店客户表-分页列表查询")
    @ApiOperation(value = "门店客户表-分页列表查询", notes = "门店客户表-分页列表查询")
    @GetMapping(value = "/listForStore")
    public Result<?> queryPageListForStore(CustomerReq customerReq,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<CustomerEntity> pageList = consumerService.pageListForStore(new Page<>(pageNo, pageSize), customerReq);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param customerEntity
     * @return
     */
    @AutoLog(value = "门店客户表-添加")
    @ApiOperation(value = "门店客户表-添加", notes = "门店客户表-添加")
    @PostMapping(value = "/addOrUpdate")
    public Result<?> addOrUpdate(@RequestBody CustomerEntity customerEntity) throws Exception {
        if (StringUtils.isBlank(customerEntity.getId())) {
            consumerService.add(customerEntity);
        }else{
            consumerService.updateById(customerEntity);
        }
        return Result.OK(customerEntity);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店客户表-通过id删除")
    @ApiOperation(value = "门店客户表-通过id删除", notes = "门店客户表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        consumerService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "门店客户表-批量删除")
    @ApiOperation(value = "门店客户表-批量删除", notes = "门店客户表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.consumerService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店客户表-通过id查询")
    @ApiOperation(value = "门店客户表-通过id查询", notes = "门店客户表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CustomerEntity customerEntity = consumerService.getById(id);
        if (customerEntity == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(customerEntity);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param customerEntity
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CustomerEntity customerEntity) {
        return super.exportXls(request, customerEntity, CustomerEntity.class, "门店客户表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CustomerEntity.class);
    }

}
